package com.whucke.im.cmd;

import com.alibaba.fastjson.JSON;
import com.whucke.im.been.LoginUser;
import com.whucke.im.been.User;
import com.whucke.im.cache.Cache;
import com.whucke.im.cache.UserCahce;
import com.whucke.im.enums.MessageType;
import com.whucke.im.http.Uri;
import com.whucke.im.service.IUserService;
import com.whucke.im.utils.HttpUtil;
import com.whucke.im.vo.JsonResult;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 命令行工具
 *
 * @author Zhang Peng
 * @since 2019/2/23 20:38
 */
@ShellComponent
public class Command {

    @Autowired
    private IUserService userService;
    @Autowired
    private HttpUtil httpUtil;

    /**
     * 用户登录
     *
     * @param username 账号
     * @param password 密码
     * @return
     */
    @ShellMethod("用户登录")
    public void login(String username, String password) {
        JsonResult result = userService.login(username, password);
        if (result.isStatus()) {
            //登录成功
            Map<String, String> vo = result.bodyToMap();
            //本地缓存登录用户
            LoginUser loginUser = new LoginUser();
            loginUser.setNickname(vo.get("nickname"));
            loginUser.setUsername(vo.get("username"));
            loginUser.setToken(vo.get("token"));
            loginUser.setId(vo.get("id"));
            UserCahce.setLoginUser(loginUser);
            Cache.CMD_PREFIX = loginUser.getUsername() + ":>";
            //绑定netty用户信息
            Map<String, Object> params = new HashMap<>();
            params.put("username", loginUser.getUsername());
            params.put("token", loginUser.getToken());
            DefaultFullHttpRequest req = httpUtil.getRequest(Uri.TOKEN_BINDING_REQ, HttpMethod.POST, params);
            Cache.socketChannel.writeAndFlush(req);
        }
        result.printResult();
    }

    /**
     * 用户注册
     *
     * @param username 账号
     * @param password 密码
     * @param nickname 昵称
     * @return
     */
    @ShellMethod("用户注册")
    public void register(String username, String password, String nickname) {
        JsonResult result = userService.register(username, password, nickname);
        result.printResult();
    }

    /**
     * 获取用户列表
     */
    @ShellMethod("用户列表")
    public void userList() {
        JsonResult result = userService.userList();
        printUserList(result);
    }

    /**
     * 选择用户
     * 在点对点发送信息之前必须先选择用户
     *
     * @param username
     */
    @ShellMethod("选择用户")
    public void selectUser(String username) {
        JsonResult result = userService.selectUser(username);
        if (!result.isStatus()) {
            result.printResult();
        } else {
            UserCahce.selectUser = username;
            User user = UserCahce.getUser(username);
            UserCahce.selectId = user.getId();
            UserCahce.sendType = MessageType.SINGLE;
            System.out.printf("返回:选择%s成功 \r\n", username);
            Cache.CMD_PREFIX = UserCahce.getLoginUser().getUsername() + "-->" + username + ":>";
        }
    }

    /**
     * 发送消息
     *
     * @param msg 消息内容
     */
    @ShellMethod("发送信息")
    public void sendMsg(String msg) {
        JsonResult result = userService.sendMsg(msg, MessageType.SINGLE);
        result.printResult();
    }

    /**
     * 打印用户列表
     *
     * @param result
     */
    private void printUserList(JsonResult result) {
        if (!result.isStatus()) {
            System.out.println(result.getMsg());
            return;
        }
        String userListJson = result.getBody().toString();
        List<User> userList = JSON.parseArray(userListJson, User.class);
        if (!CollectionUtils.isEmpty(userList)) {
            //缓存本地
            UserCahce.cacheUserList(userList);
            StringBuilder sb = new StringBuilder("返回:用户列表[");
            for (User user : userList) {
                sb.append(user.getUsername()).append(" ");
            }
            sb.append("]");
            System.out.println(sb.toString());
        } else {
            System.out.println("返回:用户列表[ ]");
        }
    }

}
